#coding=utf-8
'''
Created on Dec 10, 2015
@author: DQA wangpeng 
'''
import data.desired_setting

import data.logging_save

import time
from appium import webdriver
import data.config
import data.get_status
import threading
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import os
import sys
from time import ctime
from cookielib import logger
reload(sys)
sys.setdefaultencoding('utf-8')

import data.logging_save
import data.Create_Log_Folder
import unittest

class guess(unittest.TestCase):
    def setUp(self):
        #startup the huawei m130 app
        self.save_log = data.Create_Log_Folder.Create('xiami_select_loop_playback')
        desired_caps = data.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        data.logging_save.logging.info("startup the app already")
        print 'startup the app already'
        
    def tearDown(self):
        self.driver.quit()
        
    def test_alert(self):
        u"""测试步骤：点击“虾米”音乐首页的“精选集”模块，然后点击进去，轮播里面的当页歌曲，再按“后退”返回按键，循环播放       测试依据：底部获取到的名字和playback界面获取名字一致"""
        #excute the test case times
        playback_times = 1
        success_times = 1
        fail_times = 1
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        print save_pic
        action = TouchAction(self.driver)
        
        alert_number = 1
        while(alert_number <=10):
            try:
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                alert = self.driver.switch_to_alert()
                alert.accept()
                alert_number = alert_number +1
                
                if alert_number == 10:
                    data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                    print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                    self.driver.get_screenshot_as_file(save_pic)
                    savenumber = savenumber + 1
                    self.fail("the network envoiroment is too bad")
                        
            except:
                el_change_to_select = self.driver.find_element_by_xpath(data.config.Get_xiami_select_menu()) 
                action.tap(el_change_to_select).perform()
                time.sleep(1)
                data.logging_save.logging.info("点击进入虾米“精选集”界面")
                print time.ctime(),"点击进入虾米“精选集”界面"
                
            try:
                WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                alert = self.driver.switch_to_alert()
                alert.accept()
                alert_number = alert_number +1
                
                if alert_number == 10:
                    data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                    print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                    self.driver.get_screenshot_as_file(save_pic)
                    savenumber = savenumber + 1
                    self.fail("the network envoiroment is too bad")
            
            except:
                for i in range(7):
                    time.sleep(10)
                    els_new_special = self.driver.find_elements_by_class_name(data.config.Get_xiami_new_special())
                    time.sleep(3)
                    action.tap(els_new_special[i]).perform()
                    #self.driver.find_element_by_xpath(data.config.Get_xiami_commend()).click()
                    data.logging_save.logging.info("点击进入“精选集”界面专辑")
                    print time.ctime(),"点击进入“精选集”界面专辑"
                    alert_number = 11
                    while(alert_number <=15):        
                        try: 
                            WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                            alert = self.driver.switch_to_alert()
                            alert.accept()
                            alert_number = alert_number +1
                            if alert_number == 10:
                                data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                                print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                                self.driver.get_screenshot_as_file(save_pic)
                                savenumber = savenumber + 1
                                self.fail("the network envoiroment is too bad")
                                
                        except:
                            #WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.Xiami_first_song_name())))    
                            els_list = self.driver.find_elements_by_class_name("UIATableCell")
                            song_length = len(els_list)
                            if song_length >=3:
                                for i in range(3):
                                    time.sleep(10)
                                    test = os.path.join(data.config.Get_xiami_speicail_xpath_tuijian()+str(i+1)+"]/UIAStaticText[1]")
                                    el_play = self.driver.find_element_by_xpath(test)
                                    action.tap(el_play).perform()
                                    time.sleep(10)
                                    
                                    playback_list_name = self.driver.find_element_by_xpath(test).text
                                    #print data.config.playback_button_songname_next_page()
                                    
                                    print "当前页面获取到的歌曲名字是:",playback_list_name
                                    data.logging_save.logging.info("当前页面获取到的歌曲名字是:"+str(playback_list_name)) 
                                    
                                    time.sleep(5)
                                    #get the playback button information
                                    
                                    get_button_name = self.driver.find_element_by_xpath(data.config.playback_button_songname_next_page()).text
                                    
                                    data.logging_save.logging.info("底部播放器获取到的歌曲名:"+str(get_button_name))
                                    print time.ctime(),"底部播放器获取到的歌曲名字是：%s"%get_button_name
                                    time.sleep(2)
                                    #loop the list playback
                                    try:
                                        self.assertIn(playback_list_name,get_button_name) 
                                        #self.assertIn(get_songname_http,playback_list_name) 
                                        print ctime(),"经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                                        data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                                        success_times = success_times + 1
                                        #self.driver.find_element_by_xpath(data.config.back_previous_menu()).click()
                                        #print 'back to the menu'
                                        data.logging_save.logging.info("contiune to test")
                                        time.sleep(5)
                                        #back to the previous page
                                        
                                    except:
                                        data.logging_save.logging.warning("底部获取名字和实际获取名字不一致")
                                        print time.ctime(),"底部获取名字和实际获取名字不一致"
                                        self.driver.get_screenshot_as_file(save_pic)
                                        savenumber = savenumber + 1
                                        self.fail("the current song name do not match the real songname after playback")                                 
                                #back to the first main
                                el_back = self.driver.find_element_by_name("back normal")
                                data.logging_save.logging.info("back to previous play list,test it again")
                                action.tap(el_back).perform()
                          
                          
                            else:
                                for i in range(song_length):
                                    time.sleep(10)
                                    test = os.path.join(data.config.Get_xiami_speicail_xpath_tuijian()+str(i+1)+"]/UIAStaticText[1]")
                                    el_play = self.driver.find_element_by_xpath(test)
                                    action.tap(el_play).perform()
                                    time.sleep(10)
                                    
                                    playback_list_name = self.driver.find_element_by_xpath(test).text
                                    #print data.config.playback_button_songname_next_page()
                                    
                                    print "当前页面获取到的歌曲名字是:",playback_list_name
                                    data.logging_save.logging.info("当前页面获取到的歌曲名字是:"+str(playback_list_name)) 
                                    
                                    time.sleep(5)
                                    #get the playback button information
                                    
                                    get_button_name = self.driver.find_element_by_xpath(data.config.playback_button_songname_next_page()).text
                                    
                                    data.logging_save.logging.info("底部播放器获取到的歌曲名:"+str(get_button_name))
                                    print time.ctime(),"底部播放器获取到的歌曲名字是：%s"%get_button_name
                                    time.sleep(2)
                                    #loop the list playback
                                    try:
                                        self.assertIn(playback_list_name,get_button_name) 
                                        #self.assertIn(get_songname_http,playback_list_name) 
                                        print ctime(),"经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                                        data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                                        success_times = success_times + 1
                                        #self.driver.find_element_by_xpath(data.config.back_previous_menu()).click()
                                        #print 'back to the menu'
                                        data.logging_save.logging.info("contiune to test")
                                        time.sleep(5)
                                        #back to the previous page
                                        
                                    except:
                                        data.logging_save.logging.warning("底部获取名字和实际获取名字不一致")
                                        print time.ctime(),"底部获取名字和实际获取名字不一致"
                                        self.driver.get_screenshot_as_file(save_pic)
                                        savenumber = savenumber + 1
                                        self.fail("the current song name do not match the real songname after playback")                                 
                                #back to the first main
                                el_back = self.driver.find_element_by_name("back normal")
                                data.logging_save.logging.info("back to previous play list,test it again")
                                action.tap(el_back).perform()    
                            
                            #exit the while loop
                            alert_number = 16

                
        
        print 'finished the test'    
if __name__ == '__main__':
    unittest.main()
    print 'finished'